﻿using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(Sidvall.LogManager.Web.Host.Startup))]

namespace Sidvall.LogManager.Web.Host
{
    public partial class Startup
    {
        #region Configuration

        public void Configuration(IAppBuilder app)
        {
            RegisterTypes();

            Sidvall.Web.Host.Startup.ConfigureAuthentication(app, Sidvall.LogManager.Configuration.SettingsManager.STSBaseAddress, Sidvall.LogManager.Security.SecurityManager.AuthorityUserInfoEndpoint,
                Sidvall.LogManager.Security.SecurityManager.AuthorityTokenEndpoint, Sidvall.LogManager.Configuration.SettingsManager.WebsiteBaseAddress, Sidvall.LogManager.Configuration.SettingsManager.STSClientId,
                Sidvall.LogManager.Configuration.SettingsManager.STSClientSecret, Sidvall.LogManager.Security.SecurityManager.ResponseType, Sidvall.LogManager.Security.SecurityManager.Scope,
                "/InvalidUser/{0}", Sidvall.LogManager.Configuration.SettingsManager.TwoFactorProvider, null, Sidvall.LogManager.Web.Host.AuthenticationTicketProvider.Current);
            ConfigureWebApi(app);
            ConfigureBundles(System.Web.Optimization.BundleTable.Bundles);
            ConfigureMvc(System.Web.Routing.RouteTable.Routes);
        }

        #endregion
        #region RegisterTypes

        private static void RegisterTypes()
        {
            // Framework
            var authorizationProvider = new Sidvall.Security.IdentityAuthorizationProvider();
            Sidvall.SystemContext.Current = new Sidvall.SystemContext()
            {
                AuthorizationManager = new Sidvall.Security.AuthorizationManager(authorizationProvider),
                AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
                //CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
                ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
                CodeManager = new Sidvall.Reflection.CodeManager(new Sidvall.Reflection.Code()),
                UserExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.UserExceptionProvider()),
                SystemExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
                //LogManager = new Sidvall.Logging.LogManager(new Sidvall.Logging.EL.Log()),
                SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SmtpClient()),
                //SmsClientManager = new Sidvall.Net.Sms.SmsClientManager(new Sidvall.Net.Sms.SmsClient()),
                //SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
                SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
                TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
                TypeCache = new Sidvall.Reflection.TypeCache(),
                UserManager = new Sidvall.Security.UserManager(authorizationProvider),
                //UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.Storage.LocalStorage()),

                // Server
                PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Web.Security.PrincipalContextProvider()),
                PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Web.Security.PrincipalProvider()),

                // Host specific
                ResourceManager = new Sidvall.Resources.ResourceManager(new Sidvall.Resources.ResourceProvider()),
                SettingsManager = new Sidvall.Configuration.SettingsManager(new Sidvall.LogManager.Web.Host.Configuration.ApplicationSettings()),
            };

            // Authorization Provider
            authorizationProvider.Validators.Add(new Sidvall.LogManager.Web.Security.UserValidator());
            authorizationProvider.Validators.Add(new Sidvall.Web.Security.UserAuthenticationMethodValidator());
            authorizationProvider.Validators.Add(new Sidvall.Web.Security.UserEmailValidator(Sidvall.LogManager.Security.SecurityManager.AuthorityUserInfoEndpoint));
            if (Sidvall.LogManager.Configuration.SettingsManager.TwoFactorProvider != Sidvall.Security.TwoFactorProvider.None)
                Sidvall.Web.SystemContext.Current.RequiredAuthenticationMethod = Sidvall.Security.AuthenticationMethods.TwoFactorAuthentication;

            // Application specific
            Sidvall.LogManager.SystemContext.Current.InitializeServerDataLayer(null);
        }

        #endregion
    }
}